﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;

namespace XNWebAPI.Controllers.Tests
{
    public class HttpHelper
    {
        private static readonly HttpClient Client = new HttpClient();

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramList"></param>
        /// <returns></returns>
        public static string Post(string url, Dictionary<string, string> paramList)
        {
            var ps = new List<KeyValuePair<string, string>>();
            foreach (var item in paramList)
            {
                ps.Add(new KeyValuePair<string, string>(item.Key, item.Value));
            }
            using (var content = new FormUrlEncodedContent(ps))
            {
                var task = Client.PostAsync(url, content);
                var json = task.Result.Content.ReadAsStringAsync().Result;
                return json;
            }
        }

        /// <summary>
        /// 获取时间戳(毫秒数)
        /// </summary>
        /// <returns>返回自1970年1月1日到目前为止的毫秒数</returns>
        public static string GetCurrentTimeMillis()
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (DateTime.Now - startTime).TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
        }
    }
}